/*Copyright 2009-2010 by Ward, David J ( pymethius@ominian.net )
Licensed work, all rights reserved.*/
Ext.namespace("wm");
Ext.namespace("wmlib");

/**
 *
 *
 *On a side note, don't trust these comments
 *
 *Inspired by http://mcdconsultingllc.com/node/22
 * !Note I am starting to have some serious concerns for the approach the blogger above and now I've taken. 
 */

Ext.namespace("Ext.ux.form");  //Bandaid fix for the missing .form attribute
Ext.ux.form.WmAuthFormPanel = Ext.extend(Ext.form.FormPanel,{
    createForm: function(){        
        delete this.initialConfig.listeners;//TODO research what consequences this has        
        return new Ext.ux.form.WmAuthBasicForm(null, this.initialConfig);
    }
});
Ext.reg("WmAuthForm", Ext.ux.form.WmAuthFormPanel);


Ext.ux.form.WmAuthBasicForm = function(el, config){
    Ext.ux.form.WmAuthBasicForm.superclass.constructor.call(this, el, config);
};

Ext.extend(Ext.ux.form.WmAuthBasicForm, Ext.form.BasicForm, {
    submit: function(options){
        console.log("WmAuthBasicForm.submit event")
        this.doAction("wmAuthSubmit", options);
        return this;
    }      
    //I don't need Load functionality at this point...so skipping that portion of the code
});


Ext.namespace("wmlib");

/**
 *
 *TODO: Flip a coin or something, should I refactor this and wmlib.* to incorporate a better form of
 *class system?
 */
wmlib.auth = function(){
    var self = this;
    var AUTHWINDOWID = "authwindow", AUTHFORMID = "authform";
    
    var handleSubmission = function(msg){
        wmlog.log(msg);
        var status = "Auth request was " + (msg.result == true ? " accepted" : " Denied");
        //TODO - Semi annoying  Ext.Msg.alert("Announcement from server", status  );
    }
    
    var submitForm = function(){
        myForm = Ext.getCmp(AUTHFORMID).getForm();
        if(myForm.isValid()){            
            wm.router.makeRequest("auth.login", myForm.getFieldValues(), handleSubmission);
        }else{
            Ext.Msg.alert("Client validation", "Please fix the highlighted fields" );         
        }
    }
    
    
    var buildFields = function(){
        return [{                                                
                    fieldLabel: "Username",
                    name : "user",
                    allowBlank: false
                },
                {
                    fieldLabel: "Password",
                    name: "password",
                    inputType: "password",
                    allowBlank: false
                    
                }]
    }
    
    var buildForm = function(){
        return [{
                id: AUTHFORMID,
               xtype:"WmAuthForm",
               defaults: {width: 120},
               defaultType: 'textfield',                
               
               buttons: [  {text:"Login",handler: submitForm } ],
               defaults: {
                            listeners: {
                                    specialkey: function(field, e){
                                                    if(e.getKey() == e.ENTER){
                                                        submitForm();
                                                    }
                                    }
                            }
                    },
                items : buildFields()
               }];
    }
    
    
    
    self.buildWindow = function(){
        wm.authwindow = new Ext.Window({
           id:AUTHWINDOWID,
           title: "Authentication required",
           frame: true,
           layout: "form",
           width: 250,
           items: buildForm()
       });
        wm.authwindow.show();
    }
    
    self.kill = function(){        
        wm.authwindow.destroy()
        wm.router.makeRequest("sys.escalate",true)
    }
    
    if( wm.router.modules["auth"] == undefined){
        wm.router.modules["auth"] = self;
    }
    
}



try{
    //TODO - Really need to fix the script.onload|..stateready mechansim in loader
    wm.loader.notify("auth");
    
    wm.auth = new wmlib.auth();
    wm.auth.buildWindow();    
}catch(authErr){    
    wmlog.error(authErr);
}
